﻿(function($){
/*
	测试插件的基类
	所有插件都继承此类的一个实例
*/
function BaseTest() {
	//测试插件是否可用
	this.enabled = true;
	//最大测试时间
	this.maxTime = 0;
	//多有定时器列表
	this.timeIDS = [];
	this.cmds = null;
}

//插件测试结束时会自动执行end方法
BaseTest.prototype.end = function(){
	var self = this;
	T.testPluginEndID = setTimeout(function(){
		self.clear();
		T.start();
	}, self.maxTime);
}

//当执行end方法时，清除可能尚未执行完的动作
BaseTest.prototype.clear = function() {
	var self = this, timeIDS = self.timeIDS;
	for(var i = 0 ; i < timeIDS.length ; i++) {
		clearTimeout(timeIDS[i]);
	}
}

/**
 *	添加动作组
 */
BaseTest.prototype.run = function() {
	var self = this, timeIDS= self.timeIDS=[], defaultTime = 500, cmds = self.cmds;
	if(typeof cmds !== 'object'){
		return self;
	}
	self.maxTime=0;
	for(var i=0; i< cmds.length; i++) {
		(function(k) {
			var o = cmds[k], time = o[0] || defaultTime;
			timeIDS.push(setTimeout(function() {
				for(var j = 1 ; j < o.length ; j++) {
					var item = o[j];
					if(item.name) {
						$(item.name).trigger(item.type);
					}else if(typeof item.fun === 'function') {
						item.fun();
					}
				}
			}, self.maxTime += time));
		})(i);
	}
	self.maxTime += T.config.testWaiteTime;
	return self;
}
var T = window.AutoTest ={
	//当前测试插件索引值
	index : 0,
	//测试插件key值数组
	testNames : [],
	//所有测试插件列表
	testPlugins: {},
	//一个测试结束定时器id
	testPluginEndID : null,
	//初始化
	init : function() {
		var self = this, 
			config = self.config,
			testCtl = $('#'+config.autoTestCtl),
			testPlugins = self.testPlugins,
			i = 1;
		//存在测试按钮区域标签则在其中自动生成相关操作按钮
		if(testCtl.length){
			for(var testName in testPlugins) {
				var testPlugin = testPlugins[testName];
				testCtl.append('<label name="'+config.autoTestCtl
					+'_checkBox"><input type="checkbox" value="'
					+testName+'" checked="'+testPlugin.enabled+'">'+testPlugin.title+'</label>'
				);
				if(i++ % config.lineCheckBoxNum == 0) {
					testCtl.append('<br>');
				}
			}
			testCtl.append('<br>');
			testCtl.append('<input type="button" value="全选" name="_all">');
			testCtl.append('<input type="button" value="反选" name="_reset">');
			testCtl.append('<input type="button" value="开始" name="_start">');
			testCtl.append('<input type="button" value="停止" name="_stop">');
			//绑定相关事件
			testCtl.unbind('mouseup').mouseup(function(e){
				var target = $(e.target).closest('[name]'), name = target.attr('name');
				if(name == '_checkBox'){
					self.stop();
				}else if(name == '_start'){
					self._start();
				}else if(name == '_stop'){
					self.stop();
				}else if(name == '_all'){
					self.stop();
					$(':checkbox',testCtl).attr('checked',true);
				}else if(name == '_reset') {
					self.stop();
					$(':checkbox',testCtl).attr('checked',false);
				}
				return false;
			});
			return true;
		}
		return false;
	},
	/*
		注册测试插件
		@string testName : 测试插件key值
		@obj cmds : 测试配置信息
		@string title : 测试插件名称[可选参数]
	*/
	addTest: function(testName, cmds, title) {
		var self = this,
			config = self.config,
			enabled= config.enableds[testName] === false ? false : true,
			test = new BaseTest();
		if(typeof testName !== 'string'
			|| self.testPlugins[testName]
			|| typeof cmds !== 'object'
		) {
			return ;
		}
		if(typeof config.start === 'function') {
			cmds= [[100, {fun: function() {
				config.start(test);
			}}]].concat(cmds);
		}
		test.cmds = cmds;
		test.enabled = enabled;
		test.title = title || testName;
		self.testPlugins[testName] = test;
		self.testNames.push(testName);
	},
	/*
		启动测试
	*/
	start: function() {
		var self= this,
			config = self.config,
			testPlugins = self.testPlugins,
			testPlugin = null,
			index= self.index,
			testNames = self.testNames,
			testName= testNames[index];
		//找不到testName说明测试结束
		if(typeof testName === 'string'){
			testPlugin= testPlugins[testName];
			self.index++;
			if(testPlugin && testPlugin.enabled){
				testPlugin.run();
				testPlugin.end();
			}else {
				T.start();
			}
		}else {
			//self.index = testNames.length为结束标志
			self.index = testNames.length;
			// 测试完毕
			if(typeof self.end === 'function'){
				self.end();
			}
		}
	},
	//停止测试
	stop : function() {
		var self= this,
			testNames = self.testNames,
			testPlugins = self.testPlugins,
			testPlugin = testPlugins[testNames[self.index-1]];
		self.index = self.testNames.length;
		if(testPlugin) {
			testPlugin.clear();
		}
		//清除结束定时器
		clearTimeout(self.testPluginEndID);
	},
	//单击开始按钮
	_start : function(){
		var self = this,
			testPlugins = self.testPlugins,
			autoTestCtl = self.config.autoTestCtl;
		//index >0 或者 index<testNames.length说明已经正在运行
		if(self.index < self.testNames.length && self.index > 0){
			return ;
		}
		self.stop();
		self.index = 0;
		for(var key in testPlugins) {
			var testPlugin = testPlugins[key];
			testPlugin.enabled = $('#'+autoTestCtl+' [value='+key+']').attr('checked');
		}
		self.start();
	}
}
$(document).ready(function() {
	setTimeout(function(){
		if(!T.init()){
			T.start();
		}
	},T.waiteTime);
});
})(jQuery);
